/*  
 * @(#) ListHelper.java Create on 2016-4-18 下午5:34:36   
 *   
 * Copyright 2016 by fibrlink.   
 */


package com.springdemo.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author charlin_yin
 * @date   2016-4-18
 */
public class ListHelper {
	
	/**
	 * List分批截取
	 * @Title: subList
	 * @data:2016-4-18下午5:54:37
	 * @author:charlin_yin
	 *
	 * @param list
	 * @param length 截取长度
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static Map<Integer, List> subList(List list,int length){
		if(list!=null && length>0){
			Map<Integer, List> map = new HashMap<Integer, List>();
			for(int i=0,j=0,k=list.size()/length;j<=k;i+=length,j++){
				List newList = new ArrayList();
				if(j==k){
					if(i==list.size()){
						break;
					}
					newList = list.subList(i, list.size());
				}else{
					newList = list.subList(i, i+length);
				}
				map.put(j, newList);
			}
			return map;
		}
		return null;
	}
	
	static int k = 0;
	
	public static void main(String[] args) {
		List test = new ArrayList();
		for (int i = 0; i < 20; i++) {
			test.add(i);
		}
		System.out.print("the orginal list: ");
		for (int i = 0; i < test.size(); i++) {
			System.out.print(test.get(i) + " ");
		}
		System.out.println();
		
		final Map map = ListHelper.subList(test, 3);
		
		for (Object key : map.keySet()) {
			   System.out.println(map.get(key) + " ");
		}
		System.out.println();
		
		
		for (int i = 0; i <= test.size()/3; i++) {
			Object object = map.get(i);
			new insertThread(object).run();
		}
	}
	
	public static class insertThread implements Runnable {
		private Object object;

		public insertThread(Object object) {
			this.object = object;
		}

		@Override
		public void run() {
			System.out.println("-----run:" + object);
		}
		
	}
}
